import { nextTick, ref, unref, watch } from "vue";
import { type CustomShareActionType, CustomShareTypeEnum, type ShareBtnItem, type UseCustomShareReturnType, type WxShareOptions } from "../types";
import { onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";


/** 微信分享 */
const wechatShareIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/wechat-share-icon.png`
/** 海报 */
const posterShareIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/poster-share-icon.png`
/** 朋友圈 */
const momentsShareIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/moments-share-icon.png`
/** 复制链接 */
const copyUrlShareIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/copyUrl-share-icon.png`
/** 插入公众号 */
const insertShareIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/insert-share-icon.png`
/** 关注公众号 */
const officialAccountIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/officialAccount-icon.png`
/** 站内链接 */
const internalLinksIcon = `https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/internal-links-icon.png`
/** 下载小程序码 */
const downloadMiniprogramCodeIcon = 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/download-miniprogram-code.png'

export const customShareButtons: ShareBtnItem[] = [
  {
    icon: wechatShareIcon,
    text: '微信',
    type: CustomShareTypeEnum.SHARE,
  },
  {
    icon: posterShareIcon,
    text: '生成海报',
    type: CustomShareTypeEnum.POSTER,
  },
  {
    icon: momentsShareIcon,
    text: '朋友圈',
    type: CustomShareTypeEnum.TIME_LINE,
  },
  {
    icon: downloadMiniprogramCodeIcon,
    text: '下载二维码',
    type: CustomShareTypeEnum.DOWNLOAD_MINIPROGRAM_CODE,
  },
  {
    icon: insertShareIcon,
    text: '插入公众号',
    type: CustomShareTypeEnum.INSERT_OFFIACCOUNT,
  },
  {
    icon: copyUrlShareIcon,
    text: '复制链接',
    type: CustomShareTypeEnum.COPY_LINK,
  },
  {
    icon: officialAccountIcon,
    text: '关注公众号',
    type: CustomShareTypeEnum.OFFIACCOUNT,
  },
  {
    icon: internalLinksIcon,
    text: '站内链接',
    type: CustomShareTypeEnum.INTERNAL_LINKS,
  }
];


export interface CustomShareOptions {
  buttons: Partial<{
    /** 普通微信分享 */
    [CustomShareTypeEnum.SHARE]: boolean
    /** 海报分享 */
    [CustomShareTypeEnum.POSTER]: boolean
    /** 朋友圈分享 */
    [CustomShareTypeEnum.TIME_LINE]: boolean
    /** 复制短链 */
    [CustomShareTypeEnum.COPY_LINK]: boolean
    /** 插入公众号 */
    [CustomShareTypeEnum.INSERT_OFFIACCOUNT]: boolean
    /** 关注公众号 */
    [CustomShareTypeEnum.OFFIACCOUNT]: boolean
    /** 站内链接 */
    [CustomShareTypeEnum.INTERNAL_LINKS]: boolean
    /** 下载小程序码 */
    [CustomShareTypeEnum.DOWNLOAD_MINIPROGRAM_CODE]: boolean
  }>,
  /** 用于禁用自定义分享的二级功能 */
  disableds?: {
    shortLink?: boolean
  }
}

export function useCustomShare(options: CustomShareOptions): UseCustomShareReturnType {
  const instanceRef = ref<Nullable<CustomShareActionType>>(null);

  async function getInstance() {
    const instance = unref(instanceRef);
    if (!instance) {
      console.log('自定义分享实例尚未获取，执行作时请确保组件已渲染！');
    }
    await nextTick();
    return instance as CustomShareActionType;
  }

  function register(instance: CustomShareActionType) {
    instanceRef.value = instance
    options.buttons && instance.setButtons(options.buttons)
    options.disableds && instance.setDisableds(options.disableds)
  }

  const methods: CustomShareActionType = {
    setButtons: async (buttons) => {
      const instance = await getInstance();
      instance.setButtons(buttons)
    },
    setDisableds: async (disableds) => {
      const instance = await getInstance();
      instance.setDisableds(disableds)
    },
    setWxShareOptions: async (op) => {
      const instance = await getInstance();
      return await instance.setWxShareOptions(op)
    },
    setMenuWxShareOptions: async op => {
      const instance = await getInstance();
      instance.setMenuWxShareOptions(op)
    },
    getCurrentWxShareOptions: () => {
      return instanceRef.value!.getCurrentWxShareOptions()
    },
    setPosterDrawData: async (data) => {
      const instance = await getInstance();
      return await instance.setPosterDrawData(data)
    },
    setShareTimelineContent: async (op) => {
      instanceRef.value?.setShareTimelineContent(op)
    },
    setShareLogId: async (shareLogId) => {
      const instance = await getInstance();
      instance.setShareLogId(shareLogId)
    },
    setInternalLinkOptions: async (options) => {
      const instance = await getInstance();
      instance.setInternalLinkOptions(options)
    },
    setMiniprogramCode: async (codeImg: string) => {
      const instance = await getInstance();
      instance.setMiniprogramCode(codeImg)
    }
  }

  return [register, methods]
}